package edu.uoc.tdp.pac4.connexiomanteniment.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


import edu.uoc.tdp.pac4.connexiomanteniment.*;
import edu.uoc.tdp.pac4.connexiomanteniment.i18n.*;
import edu.uoc.tdp.pac4.connexiomanteniment.exceptions.*;
import edu.uoc.tdp.pac4.server.connexiomanteniment.db.GestorUniversitats;
import java.util.Vector;
import java.util.Iterator;

import javax.swing.SwingConstants;
import javax.swing.JCheckBox;


public class PantallaGestioUsuaris extends JFrame {

	private int operation;  // indica quina operacio estem fent: alta, baixa, modificacio
	private static final int ALTA = 1;
	private static final int BAIXA = 2;
	private static final int MODIFICACIO = 3;
	private static final int SHORTLENGTH=16;
	private static final int MEDIUMLENGTH=32;
	private static final int LONGLENGTH=64;
	private static final int VERYLONGLENGTH=100;
	
	
	private static final long serialVersionUID = 3065279689452874985L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JCheckBox chckbxNewCheckBox;
	private JCheckBox chckbxNewCheckBox_1;
	private JComboBox comboBox;
	private JComboBox comboBox_1;	
	private JComboBox comboBox_2;
	private JButton btnNewButton_1;
	private JLabel lblNewLabel_3; 	
    private JLabel lblProfile;
    private JLabel label_2;
    private JLabel label_3;
    private JLabel lblFirstName;
    private JLabel lblLastName;
    private JPanel panel;
    private JLabel label_1;
    private JButton btnNewButton;
    private JButton button;
    private JButton button_1;
    private JButton button_2;
    private JLabel label;
    private JLabel label_4;
    private JLabel lblNewLabel;
    private JLabel lblNewLabel_1;
    private JLabel lblNewLabel_2;
    private JLabel lblNewLabel_4;
    private JLabel lblNewLabel_5;
    private JLabel lblNewLabel_6;
    private JLabel lblNewLabel_7;
    private JLabel lblNewLabel_8;
    private JLabel lblNewLabel_9;
    private PantallaGestioUsuaris p; 
    private JButton btnNewButton_2;
    
    
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					PantallaGestioUsuaris frame = new PantallaGestioUsuaris();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PantallaGestioUsuaris() {
		p = this;
		setResizable(false);
		LanguageUtils.setDefaultLanguage("i18n/messagesConnexioManteniment");

		setTitle(LanguageUtils.getMessage("PantallaGestioUsuaris.usersmanagementTitle.text") + " - " + LanguageUtils.getMessage("PantallaGestioUsuaris.loggedAs.text") + ": " + Usuari.usuariConnectat.getNom() + " " + Usuari.usuariConnectat.getCognom1());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 596, 458);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		label_2 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.loginLabel.text"));
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(42, 122, 74, 19);
		contentPane.add(label_2);
		label_2.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		
		label_3 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.passwordLabel.text"));
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(25, 150, 91, 20);
		contentPane.add(label_3);
		label_3.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		
		textField = new JTextField();
		textField.setBounds(127, 122, 278, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(127, 150, 278, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(127, 178, 278, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		lblFirstName = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.firstnameLabel.text"));
		lblFirstName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFirstName.setBounds(42, 180, 74, 19);
		contentPane.add(lblFirstName);
		lblFirstName.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		
		textField_3 = new JTextField();
		textField_3.setBounds(127, 204, 134, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		lblLastName = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.lastnameLabel.text"));
		lblLastName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLastName.setBounds(13, 205, 103, 19);
		contentPane.add(lblLastName);
		lblLastName.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		
		fillRolesComboBox();
		comboBox.setBounds(127, 231, 278, 20);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) { p.enableUniversity(comboBox.getSelectedItem().equals(LanguageUtils.getMessage("PantallaGestioUsuaris.roleEstudiant.text"))); } // aixo o preguntar que l'index sigui 1
		});
		contentPane.add(comboBox);
		
		lblProfile = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.profileLabel.text"));
		lblProfile.setHorizontalAlignment(SwingConstants.RIGHT);
		lblProfile.setBounds(13, 231, 103, 20);
		contentPane.add(lblProfile);
		lblProfile.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		
		panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 0, 596, 76);
		contentPane.add(panel);
		
		label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/logo.png")));
		label_1.setBounds(484, 6, 93, 55);
		panel.add(label_1);
		
		btnNewButton = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.newButton.text"));
		btnNewButton.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/user1_add.png")));
		btnNewButton.setBounds(16, 22, 117, 29);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				operation = ALTA;
				resetAllFieldsForCreation();
				showOperacioRealitzada(false);
			}
		});
		panel.add(btnNewButton);
		
		button = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.saveButton.text"));
		button.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/user1_new.png")));
		button.setBounds(132, 22, 117, 29);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean ok = checkMandatoryFieldsNotEmpty();
				if (ok) ok = checkFieldsLength();
				if (ok) {
					GestorAcces ga = new GestorAcces();
					Usuari u = ga.initUsuari(p);
					try {
						if (operation == ALTA) { int id = ga.altaUsuari(u); p.setIdIntern(id); if (id != -1) { operation = MODIFICACIO; p.enableActive(true); } }
						if (operation == BAIXA) ga.baixaUsuari(u);
						if (operation == MODIFICACIO) ga.modificacioUsuari(u);
						displayUsuari(u);
						showOperacioRealitzada(true);
					} catch (Exception e) {}
				}
			}
		});
		panel.add(button);
		
		button_1 = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.searchButton.text"));
		button_1.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/user1_find.png")));
		button_1.setBounds(250, 22, 117, 29);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showOperacioRealitzada(false);
				PantallaCercaUsuaris pC = new PantallaCercaUsuaris(p);
				pC.setVisible(true);
			}
		});
		panel.add(button_1);
		
		button_2 = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.removeButton.text"));
		button_2.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/user1_delete.png")));
		button_2.setBounds(368, 22, 117, 29);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				operation = BAIXA;
				p.setEstatEnSistema('i');
				GestorAcces ga = new GestorAcces();
				Usuari u = ga.initUsuari(p);
				try { ga.baixaUsuari(u); displayUsuari(u); showOperacioRealitzada(true); } catch (Exception e) {}
				operation = MODIFICACIO;
			}
		});
		panel.add(button_2);
		
		label = new JLabel("by SoftWebSolutions");
		label.setFont(new Font("Calibri", Font.PLAIN, 9));
		label.setBounds(38, 381, 84, 14);
		contentPane.add(label);
		
		label_4 = new JLabel("");
		label_4.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/logo_empresa.png")));
		label_4.setBounds(6, 378, 27, 14);
		contentPane.add(label_4);
		
		lblNewLabel = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.idLabel.text"));
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(25, 94, 91, 16);
		contentPane.add(lblNewLabel);
		
		textField_4 = new JTextField();
		textField_4.setEditable(false);
		textField_4.setBounds(127, 88, 134, 28);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		textField_4.enableInputMethods(false);
		
		lblNewLabel_1 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.documentLabel.text"));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(42, 261, 74, 16);
		contentPane.add(lblNewLabel_1);
		
		textField_5 = new JTextField();
		textField_5.setBounds(127, 255, 134, 28);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		lblNewLabel_2 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.documenttypeLabel.text"));
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(13, 290, 103, 16);
		contentPane.add(lblNewLabel_2);
		
		// comboBox_1 = new JComboBox();
		fillDocTypesComboBox();
		comboBox_1.setBounds(127, 285, 134, 27);
		contentPane.add(comboBox_1);
		
		lblNewLabel_3 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.universityLabel.text"));
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_3.setBounds(42, 346, 74, 16);
		contentPane.add(lblNewLabel_3);
		
		comboBox_2 = new JComboBox();
		comboBox_2.setEditable(true);
		comboBox_2.setBounds(127, 340, 278, 27);
		contentPane.add(comboBox_2);
		fillUniversitiesComboBox();
		
		lblNewLabel_4 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.emailLabel.text"));
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_4.setBounds(42, 318, 74, 16);
		contentPane.add(lblNewLabel_4);
		
		textField_6 = new JTextField();
		textField_6.setBounds(127, 313, 278, 28);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		lblNewLabel_5 = new JLabel (LanguageUtils.getMessage("PantallaGestioUsuaris.statusLabel.text"));
		lblNewLabel_5.setBounds(412, 94, 74, 16);
		contentPane.add(lblNewLabel_5);
		
		lblNewLabel_6 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.creationDateLabel.text"));
		lblNewLabel_6.setBounds(434, 157, 103, 16);
		contentPane.add(lblNewLabel_6);
		
		textField_7 = new JTextField();
		textField_7.setEditable(false);
		textField_7.setBounds(429, 174, 134, 28);
		contentPane.add(textField_7);
		textField_7.setColumns(10);
		
		lblNewLabel_7 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.modificationDateLabel.text"));
		lblNewLabel_7.setBounds(434, 209, 129, 16);
		contentPane.add(lblNewLabel_7);
		
		textField_8 = new JTextField();
		textField_8.setEditable(false);
		textField_8.setBounds(429, 226, 134, 28);
		contentPane.add(textField_8);
		textField_8.setColumns(10);
		
		lblNewLabel_8 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.deletionDateLabel.text"));
		lblNewLabel_8.setBounds(434, 261, 103, 16);
		contentPane.add(lblNewLabel_8);
		
		textField_9 = new JTextField();
		textField_9.setEditable(false);
		textField_9.setBounds(429, 278, 134, 28);
		contentPane.add(textField_9);
		textField_9.setColumns(10);
		
		btnNewButton_1 = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.listoftestsButton.text"));
		btnNewButton_1.setIcon(new ImageIcon(PantallaGestioUsuaris.class.getResource("/images/document_preferences.png")));
		btnNewButton_1 = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.listoftestsButton.text"));
		btnNewButton_1.setBounds(417, 341, 160, 29);
		contentPane.add(btnNewButton_1);
		
		chckbxNewCheckBox = new JCheckBox(LanguageUtils.getMessage("PantallaGestioUsuaris.activeButton.text"));
		chckbxNewCheckBox.setBounds(474, 81, 109, 23);
		contentPane.add(chckbxNewCheckBox);
		
		chckbxNewCheckBox_1 = new JCheckBox(LanguageUtils.getMessage("PantallaGestioUsuaris.blockedButton.text"));
		chckbxNewCheckBox_1.setBounds(474, 106, 109, 23);
		contentPane.add(chckbxNewCheckBox_1);

		textField_10 = new JTextField();
		textField_10.setBounds(263, 204, 134, 20);
		contentPane.add(textField_10);
		textField_10.setColumns(10);
		
//		lblNewLabel_9 = new JLabel("");
		lblNewLabel_9 = new JLabel(LanguageUtils.getMessage("PantallaGestioUsuaris.operacioRealitzada.text"));
		lblNewLabel_9.setVisible(false);
		lblNewLabel_9.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_9.setForeground(Color.BLUE);
		lblNewLabel_9.setBounds(137, 378, 278, 17);
		contentPane.add(lblNewLabel_9);
		
		btnNewButton_2 = new JButton(LanguageUtils.getMessage("PantallaGestioUsuaris.exitButton.text"));
		btnNewButton_2.setBounds(417, 366, 160, 29);
		btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.exit(0);	} });
		contentPane.add(btnNewButton_2);
		
		operation = ALTA; resetAllFieldsForCreation();
		enableInternalIdentifier(false);

	}
	
	private void fillRolesComboBox() {
		String[] elsRols = new String[4];
		elsRols[0] = "";
		elsRols[1] = LanguageUtils.getMessage("PantallaGestioUsuaris.roleEstudiant.text");
		elsRols[2] = LanguageUtils.getMessage("PantallaGestioUsuaris.roleAdministrador.text");
		elsRols[3] = LanguageUtils.getMessage("PantallaGestioUsuaris.roleResponsableEvents.text");
		comboBox = new JComboBox(elsRols);
	}

	private void fillDocTypesComboBox() {
		String[] elsTipusDocument = new String[5];
		elsTipusDocument[0] = "";
		elsTipusDocument[1] = LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIF.text");
		elsTipusDocument[2] = LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIE.text");
		elsTipusDocument[3] = LanguageUtils.getMessage("PantallaGestioUsuaris.docTypePassaport.text");
		elsTipusDocument[4] = LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeAltres.text");
		comboBox_1 = new JComboBox(elsTipusDocument);		
	}
	
	private void fillUniversitiesComboBox() {
		GestorUniversitats guni = new GestorUniversitats();
		Vector<String> v = null;
		try { v = guni.getUniversitats(); } catch (Exception e) { }
		String[] universityNames = new String[v.size() + 1];
		universityNames[0] = "";
		Iterator<String> it = v.iterator();
		comboBox_2.removeAllItems(); comboBox_2.addItem("");
		while (it.hasNext()) { comboBox_2.addItem(it.next()); }
	}
	
	public Integer getIdIntern() { String s = textField_4.getText(); return (s == null || "".equals(s.trim())? null : new Integer(s.trim()));  }
	public void setIdIntern(Integer pIdIntern) { String s = (pIdIntern == null? "" : pIdIntern.toString()); textField_4.setText(s); }
	public String getIdUsuari() { return (textField.getText() == null ? null : textField.getText().trim()); } 
	public void setIdUsuari(String pIdUsuari) { textField.setText(pIdUsuari == null ? "" : pIdUsuari);}
	public String getPwd() { return (textField_1.getText() == null ? null : textField_1.getText().trim()); }
	public void setPwd(String pPwd) { textField_1.setText(pPwd == null ? "" : pPwd); }
	public String getNom() { return (textField_2.getText() == null ? null : textField_2.getText().trim()); }
	public void setNom(String pNom) { textField_2.setText(pNom == null ? "" : pNom); }
	public String getCognom1() { return (textField_3.getText() == null ? null : textField_3.getText().trim()); }
	public void setCognom1(String pCognom1) { textField_3.setText(pCognom1 == null ? "" : pCognom1); }
	public String getCognom2() { return (textField_10.getText() == null ? null : textField_10.getText().trim()); }
	public void setCognom2(String pCognom2) { textField_10.setText(pCognom2 == null ? "" : pCognom2); }
	public String getMail() { return (textField_6.getText() == null ? null : textField_6.getText().trim()); }
	public void setMail(String pEmail) { textField_6.setText(pEmail == null ? "" : pEmail); }
	public String getDocument() { return (textField_5.getText() == null ? null : textField_5.getText().trim()); }
	public void setDocument(String pDocument) { textField_5.setText(pDocument == null ? "" : pDocument); }
    public String getTipusDocument() { 
		String value = (String)comboBox_1.getSelectedItem();
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIF.text"))) return "NIF";
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIE.text"))) return "NIE";
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypePassaport.text"))) return "PAS";
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeAltres.text"))) return "ALT";
		// mai hauriem d'arribar aqui
//		try { throw new ExceptionMantenimentUsuari("PantallaError.ErrorMantenimentUsuaris.unexpectedDocTypeValue.text"); } catch (Exception e) {} 
		return "";
    }
    public void setTipusDocument(String pTipusDocument) {
    	if (pTipusDocument == null) try { throw new ExceptionMantenimentUsuari("PantallaError.ErrorMantenimentUsuaris.unexpectedDocTypeValue.text"); } catch (Exception e) {} 
        if (pTipusDocument.equals("NIF")) { comboBox_1.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIF.text")); return; }
        if (pTipusDocument.equals("NIE")) { comboBox_1.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeNIE.text")); return; }
        if (pTipusDocument.equals("PAS")) { comboBox_1.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypePassaport.text")); return; }
        if (pTipusDocument.equals("ALT")) { comboBox_1.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.docTypeAltres.text")); return; }
        if (pTipusDocument.equals("")) { comboBox_1.setSelectedItem(""); return; }
		// mai hauriem d'arribar aqui
//		try { throw new ExceptionMantenimentUsuari("PantallaError.ErrorMantenimentUsuaris.unexpectedDocTypeValue.text"); } catch (Exception e) {} 			
    }
    public char getEstatEnSistema() { 
    	boolean active = chckbxNewCheckBox.isSelected(); if (!active) return 'i';
    	boolean locked = chckbxNewCheckBox_1.isSelected(); return (locked ? 'b' : 'a');
    	// 'a' per actiu, 'i' per inactiu, 'b' per bloquejat
    }
    public void setEstatEnSistema(char pEstat) {
    	switch (pEstat) {
    	case 'a' : chckbxNewCheckBox.setSelected(true); chckbxNewCheckBox_1.setSelected(false); break;
    	case 'i' : chckbxNewCheckBox.setSelected(false); chckbxNewCheckBox_1.setSelected(false); break;
    	case 'b' : chckbxNewCheckBox.setSelected(true); chckbxNewCheckBox_1.setSelected(true); break;
    	default  : chckbxNewCheckBox.setSelected(false); chckbxNewCheckBox_1.setSelected(false);
    	}
    }
//	public Date getDataCreacio() { String s = textField_7.getText(); return ((s == null || "".equals(s.trim())? null : new Date(s.trim()))); } // ESTIC AQUI 
    private void setData(Date pDate, JTextField pTextField) {
    	if (pDate == null) { pTextField.setText(null); return; }
        DateFormat dateFormatter;
        dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
        pTextField.setText(dateFormatter.format(pDate));    
    }
	public void setDataCreacio(Date pDataCreacio) { setData(pDataCreacio,textField_7);    }
//	public Date getDataModificacio() { return dataModificacio; } 
	public void setDataModificacio(Date pDataModificacio) { setData(pDataModificacio,textField_8); } 
//	public Date getDataEsborrat() { return dataEsborrat; } 
	public void setDataEsborrat(Date pDataEsborrat) { setData(pDataEsborrat,textField_9); } 
//	public Administrador getUsuariCreacio() { return usuariCreacio; } 
//	public void setUsuariCreacio(Administrador pUsuariCreacio) { usuariCreacio = pUsuariCreacio; } 
//	public Administrador getUsuariModificacio() { return usuariModificacio; } 
//	public void setUsuariModificacio(Administrador pUsuariModificacio) { usuariModificacio = pUsuariModificacio; } 
//	public Administrador getUsuariEsborrat() { return usuariEsborrat; } 
//	public void setUsuariEsborrat(Administrador pUsuariEsborrat) { usuariEsborrat = pUsuariEsborrat; }
	public char getTipusUsuari() {
		String value = (String)comboBox.getSelectedItem();
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.roleEstudiant.text"))) return 'E';
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.roleAdministrador.text"))) return 'A';
		if (value.equals(LanguageUtils.getMessage("PantallaGestioUsuaris.roleResponsableEvents.text"))) return 'R';
		// mai hauriem d'arribar aqui
//		try { throw new ExceptionMantenimentUsuari("PantallaError.ErrorMantenimentUsuaris.unexpectedRoleValue.text"); } catch (Exception e) {} 
		return 'c';
	}
	public void setTipusUsuari(char pTipusUsuari) {
		switch (pTipusUsuari) {
		case 'A' : comboBox.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.roleAdministrador.text")); break;  
		case 'E' : comboBox.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.roleEstudiant.text")); break;  
		case 'R' : comboBox.setSelectedItem(LanguageUtils.getMessage("PantallaGestioUsuaris.roleResponsableEvents.text")); break;
		case ' ' : comboBox.setSelectedItem(LanguageUtils.getMessage("")); break; 
		default:  // mai hauriem d'arribar aqui
//			try { throw new ExceptionMantenimentUsuari("PantallaError.ErrorMantenimentUsuaris.unexpectedRoleValue.text"); } catch (Exception e) {} 			
		}
	}
	public String getUniversitat() { return (String)comboBox_2.getSelectedItem() == null ? null : ((String)comboBox_2.getSelectedItem()).trim(); }
	public void setUniveritat(String pUniversitat) { comboBox_2.setSelectedItem(pUniversitat); }
	
	public void enableInternalIdentifier(boolean b) { lblNewLabel.setEnabled(b); textField_4.setEnabled(b); }
	public void enableLogin(boolean b) { label_2.setEnabled(b); textField.setEnabled(b); }
	public void enablePassword(boolean b) { label_3.setEnabled(b); textField_1.setEnabled(b); }
	public void enableName(boolean b) { lblFirstName.setEnabled(b); textField_2.setEnabled(b); }
	public void enableFamilyNames(boolean b) { lblLastName.setEnabled(b); textField_3.setEnabled(b); textField_10.setEnabled(b); }
	public void enableRole(boolean b) { lblProfile.setEnabled(b); comboBox.setEnabled(b); }
	public void enableDocument(boolean b) { lblNewLabel_1.setEnabled(b); lblNewLabel_2.setEnabled(b); textField_5.setEnabled(b); comboBox_1.setEnabled(b); }
	public void enableEmail(boolean b) { lblNewLabel_4.setEnabled(b); textField_6.setEnabled(b); }
	public void enableUniversity(boolean b) { lblNewLabel_3.setEnabled(b); comboBox_2.setEnabled(b); }
	public void enableActive(boolean b) { chckbxNewCheckBox.setEnabled(b); }
	public void enableLocked(boolean b) { chckbxNewCheckBox_1.setEnabled(b); }
	public void enableDates(boolean b) { lblNewLabel_6.setEnabled(b); lblNewLabel_7.setEnabled(b); lblNewLabel_8.setEnabled(b); textField_7.setEnabled(b); textField_8.setEnabled(b); textField_9.setEnabled(b); }
	public void enableStudentTests(boolean b) { btnNewButton_1.setEnabled(b); }

	public void displayUsuari(Usuari u) {
		setIdIntern(u.getIdIntern()); setIdUsuari(u.getIdUsuari()); setPwd(u.getPwd()); setMail(u.getMail()); 
		setNom(u.getNom()); setCognom1(u.getCognom1()); setCognom2(u.getCognom2());
		setTipusUsuari(u.getTipusUsuari()); setTipusDocument(u.getTipusDocumentId());
		setDocument(u.getDocument()); setEstatEnSistema(u.getEstatEnSistema());
		setDataCreacio(u.getDataCreacio()); setDataModificacio(u.getDataModificacio()); setDataEsborrat(u.getDataEsborrat());
		enableInternalIdentifier(false);
		boolean e = (u.getTipusUsuari() == 'E'); boolean a = (u.getEstatEnSistema() != 'i');
		enableLogin(a); enablePassword(a); enableName(a); enableFamilyNames(a);
		enableRole(false); enableDocument(a); enableEmail(a); enableUniversity(a && e);
		enableActive(true); enableLocked(a); enableDates(false); enableStudentTests(a && e);
	}
	
	public void loadUsuari(String login) {
		try { displayUsuari(new GestorAcces().getUsuari(login)); operation = MODIFICACIO;} catch (ExceptionConnexioManteniment e) {}
	}
	
    private void resetAllFieldsForCreation() {
		setIdIntern(null); setIdUsuari(""); setPwd(""); setMail(""); 
		setNom(""); setCognom1(""); setCognom2("");
		setTipusUsuari(' '); setTipusDocument("");
		setDocument(""); setEstatEnSistema('a'); chckbxNewCheckBox.setEnabled(false);
		setDataCreacio(new Date()); 
		setDataModificacio(null); setDataEsborrat(null); 
		fillUniversitiesComboBox();
		enableLogin(true); enablePassword(true); enableName(true); enableFamilyNames(true);
		enableRole(true); enableDocument(true); enableEmail(true); enableUniversity(false);
		enableActive(false); enableLocked(true); enableDates(false); enableStudentTests(false);
    }
    
    private boolean checkMandatoryFieldsNotEmpty() {
    	boolean ok = true;
    	char t = getTipusUsuari();
    	try {
    		if (t == 'E') {
        		if (getUniversitat() == null || "".equals(getUniversitat().trim())) {
        			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.universitat.text"); 
        			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.universitat.text"); 
        			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.universitat.text");     			
        		}    			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getMail() == null || "".equals(getMail().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.mail.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.mail.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.mail.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getDocument() == null || "".equals(getDocument().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.document.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.document.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.document.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		String td = getTipusDocument();
    		if (!"NIF".equals(td) && !"NIE".equals(td) && !"PAS".equals(td) && !"ALT".equals(td)) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.tipusDocument.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.tipusDocument.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.tipusDocument.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (t != 'A' && t != 'E' && t != 'R') {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.rol.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.rol.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.rol.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getCognom1() == null || "".equals(getCognom1().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.cognom1.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.cognom1.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.cognom1.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getNom() == null || "".equals(getNom().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.nom.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.nom.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.nom.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getPwd() == null || "".equals(getPwd().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.password.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.password.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.password.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getIdUsuari() == null || "".equals(getIdUsuari().trim())) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.emptyMandatoryField.idusuari.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.idusuari.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.idusuari.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getIdIntern() == null) {
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.emptyMandatoryField.internalid.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.emptyMandatoryField.internalid.text"); 
    		}
    	} catch (Exception e) { ok = false; }    	
    	return ok;
    }
	
    private boolean checkFieldsLength() {
    	boolean ok = true;
    	char t = getTipusUsuari();
    	try {
    		if (t == 'E') {
        		if (getUniversitat().length() > VERYLONGLENGTH) {
        			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.universitat.text"); 
        			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.universitat.text"); 
        			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.universitat.text");     			
        		}    			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getMail().length() > LONGLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.mail.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.mail.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.mail.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getDocument().length() > SHORTLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.document.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.document.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.document.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getCognom2().length() > MEDIUMLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.cognom2.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.cognom2.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.cognom2.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getCognom1().length() > MEDIUMLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.cognom1.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.cognom1.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.cognom1.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getNom().length() > MEDIUMLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.nom.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.nom.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.nom.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getPwd().length() > SHORTLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.password.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.password.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.password.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	try {
    		if (getIdUsuari().length() > SHORTLENGTH) {
    			if (operation == ALTA) throw new ExceptionAltaUsuari("PantallaError.ErrorAlta.valueTooLong.idusuari.text"); 
    			if (operation == BAIXA) throw new ExceptionBaixaUsuari("PantallaError.ErrorBaixa.valueTooLong.idusuari.text"); 
    			if (operation == MODIFICACIO) throw new ExceptionMantenimentUsuari("PantallaError.ErrorModificacio.valueTooLong.idusuari.text");     			
    		}
    	} catch (Exception e) { ok = false; }
    	return ok;
    }
    
    private void showOperacioRealitzada(boolean b) {
		lblNewLabel_9.setVisible(b);
    }
} 
